package cn.donting.jme.expansion.inspect;

import cn.donting.jme.EditorContent;
import cn.donting.jme.editor.scene.SceneCamera;
import cn.donting.jme.event.spatial.SceneCameraChangeEvent;
import cn.donting.jme.expansion.attribute.BaseInfoAttributeGroup;
import cn.donting.jme.expansion.attribute.TitlePaneAttributeGroup;
import cn.donting.jme.expansion.attribute.lmpl.BooleanAttribute;
import cn.donting.jme.expansion.attribute.lmpl.ColorAttribute;
import cn.donting.jme.expansion.attribute.lmpl.EnumAttribute;
import cn.donting.jme.expansion.attribute.lmpl.Vector3fAttribute;
import cn.donting.jme.inspect.Inspect;
import cn.donting.jme.ioc.Bean;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;

@Bean
public class SceneCamraeInspectBuilder implements InspectBuilder<SceneCamera> {
    @Override
    public boolean isHandle(Object o) {
        return o instanceof SceneCamera;
    }

    @Override
    public void builder(Inspect inspect, SceneCamera sceneCamera) throws Exception {
        BaseInfoAttributeGroup basInfoGroupBuilder = new BaseInfoAttributeGroup();
        basInfoGroupBuilder.setName(sceneCamera.getCamera().getName());
        basInfoGroupBuilder.setIcon(new FlatSVGIcon("icon/camera.svg", BaseInfoAttributeGroup.ICON_SIZE, BaseInfoAttributeGroup.ICON_SIZE));
        basInfoGroupBuilder.setType(sceneCamera.getCamera().getClass().getName());
        basInfoGroupBuilder.setNameChange(newValue -> {
            sceneCamera.getCamera().setName(newValue);
            SceneCameraChangeEvent sceneCameraChangeEvent = new SceneCameraChangeEvent(basInfoGroupBuilder, sceneCamera);
            EditorContent.postEvent(sceneCameraChangeEvent);
        });
        inspect.setTop(basInfoGroupBuilder);


        inspect.addAttributeGroup(createBaseInfo(sceneCamera));
        inspect.addAttributeGroup(createTransform(sceneCamera));

    }
    private  TitlePaneAttributeGroup createBaseInfo(SceneCamera sceneCamera){
        TitlePaneAttributeGroup titlePaneAttributeGroup=new TitlePaneAttributeGroup("基础信息");
        Camera camera = sceneCamera.getCamera();
        titlePaneAttributeGroup.setExpand(true);

        EnumAttribute<SceneCamera.CameraPos> viewPortTypeAttribute  = new EnumAttribute("viewPortType:",SceneCamera.CameraPos.class);
        viewPortTypeAttribute.setValue(sceneCamera.getCameraPos());
        viewPortTypeAttribute.setValueChangeListener(newValue -> {
            sceneCamera.setCameraPos(newValue);
        });
        titlePaneAttributeGroup.addAttribute(viewPortTypeAttribute);

        BooleanAttribute viewPortEnabledAttribute  = new BooleanAttribute("enabled:");
        viewPortEnabledAttribute.setValue(sceneCamera.isViewPortEnabled());
        viewPortEnabledAttribute.setValueChangeListener(newValue -> {
            sceneCamera.setViewPortEnabled(newValue);
        });
        titlePaneAttributeGroup.addAttribute(viewPortEnabledAttribute);

        ColorAttribute colorAttribute=new ColorAttribute("backColor");
        colorAttribute.setValue(sceneCamera.getViewPortBackColor());
        colorAttribute.setValueChangeListener(newValue -> {
            sceneCamera.setViewPortBackColor(newValue);
        });
        titlePaneAttributeGroup.addAttribute(colorAttribute);


        BooleanAttribute viewPortClearColorAttribute  = new BooleanAttribute("clearColor:");
        viewPortClearColorAttribute.setValue(sceneCamera.isViewPortClearColor());
        viewPortClearColorAttribute.setValueChangeListener(newValue -> {
            sceneCamera.setViewPortClearColor(newValue);
        });
        titlePaneAttributeGroup.addAttribute(viewPortClearColorAttribute);

        BooleanAttribute clearDepthAttribute  = new BooleanAttribute("clearDepth:");
        clearDepthAttribute.setValue(sceneCamera.isViewPortClearDepth());
        clearDepthAttribute.setValueChangeListener(newValue -> {
            sceneCamera.setViewPortClearDepth(newValue);
        });
        titlePaneAttributeGroup.addAttribute(clearDepthAttribute);


        BooleanAttribute clearStencilAttribute  = new BooleanAttribute("clearStencil:");
        clearStencilAttribute.setValue(sceneCamera.isViewPortClearStencil());
        clearStencilAttribute.setValueChangeListener(newValue -> {
            sceneCamera.setViewPortClearStencil(newValue);
        });
        titlePaneAttributeGroup.addAttribute(clearStencilAttribute);

        return titlePaneAttributeGroup;
    }

    private  TitlePaneAttributeGroup createTransform(SceneCamera sceneCamera){
        TitlePaneAttributeGroup titlePaneAttributeGroup=new TitlePaneAttributeGroup("transform");
        titlePaneAttributeGroup.setTitleIcon(new FlatSVGIcon("icon/transform.svg", 16, 16));
        Camera camera = sceneCamera.getCamera();
        Vector3f location = camera.getLocation();
        Quaternion rotation = camera.getRotation();

        Vector3fAttribute locationAttribute = new Vector3fAttribute();
        locationAttribute.setValue(location);
        locationAttribute.setTitle("location");
        titlePaneAttributeGroup.addAttribute(locationAttribute);
        locationAttribute.setValueChangeListener(newValue -> {
            camera.setLocation(newValue);
        });


        Vector3fAttribute rotationAttribute = new Vector3fAttribute();
        rotationAttribute.setValue(location);
        rotationAttribute.setTitle("rotation");
        titlePaneAttributeGroup.addAttribute(rotationAttribute);
        float[] angles = rotation.toAngles(new float[3]);
        rotationAttribute.setValue(new Vector3f(angles[0] / FastMath.PI * 180, angles[1] / FastMath.PI * 180, angles[2] / FastMath.PI * 180));
        rotationAttribute.setValueChangeListener((newValue) -> {
            EditorContent.runJme(() -> {
                Quaternion q = new Quaternion();
                q.fromAngles(newValue.x / 180 * FastMath.PI, newValue.y / 180 * FastMath.PI, newValue.z / 180 * FastMath.PI);
                camera.setRotation(q);
            });
        });
        return titlePaneAttributeGroup;
    }
}
